//
//  FirstCollectionViewCellLayout.m
//  Homework_19
//
//  Created by 徐昊 on 16/8/10.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "FirstCollectionViewCellLayout.h"

@interface FirstCollectionViewCellLayout ()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributesArray;
@end

@implementation FirstCollectionViewCellLayout
- (instancetype)init {
    self = [super init];
    if (self) {
        _attributesArray = [NSMutableArray array];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        _attributesArray = [NSMutableArray array];
    }
    return self;
}

- (void)prepareLayout {
    CGFloat x = 0;
    CGFloat y = 0;
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        x = CGRectGetWidth(self.collectionView.bounds) * (i % 5);
        y = CGRectGetHeight(self.collectionView.bounds) * (i / 5);
        CGFloat width = CGRectGetWidth(self.collectionView.bounds);
        CGFloat height = CGRectGetHeight(self.collectionView.bounds);
        layoutAttributes.frame = CGRectMake(x, y, width, height);
        [_attributesArray addObject:layoutAttributes];
    }
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _attributesArray;
}
- (CGSize)collectionViewContentSize {
    return CGSizeMake(CGRectGetWidth(self.collectionView.bounds), 0);
}
@end
